package com.gewu.bookeeping.View.adapter;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gewu.bookeeping.MyDb.bean.WeChtBeanIn;
import com.gewu.bookeeping.MyDb.bean.WeChtBeanOut;
import com.gewu.bookeeping.OnClick.goBack;
import com.gewu.bookeeping.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 主页面三个fragment的适配器
 */
public class OneItemAdapter extends BaseExpandableListAdapter {

    private static String[] fatherText = {"支出", "收入"};
    private List<List<Object>> ChildTexts = new ArrayList<>();
    List<Object> text1 = new ArrayList<>();
    List<Object> text2 = new ArrayList<>();
    private goBack goBack;

    public OneItemAdapter(goBack goBack) {
        ChildTexts.add(text1);
        ChildTexts.add(text2);
        this.goBack= goBack;
    }

    //微信支出
    public void text1Add(WeChtBeanOut weChtBeanIn) {
        for (int i = 0; i < text1.size(); i++) {
            WeChtBeanOut weChtBeanIns = (WeChtBeanOut) text1.get(i);
            if (weChtBeanIns.getTime().equals(weChtBeanIn.getTime())) {
                return;
            }
        }
        text1.add(weChtBeanIn);
    }

    //微信收入
    public void text2Add(WeChtBeanIn weChtBeanIn) {
        for (int i = 0; i < text2.size(); i++) {
            WeChtBeanIn weChtBeanIns = (WeChtBeanIn) text2.get(i);
            if (weChtBeanIns.getTime().equals(weChtBeanIn.getTime())) {
                return;
            }
        }
        text2.add(weChtBeanIn);
    }


    @Override
    public int getGroupCount() {
        return fatherText.length;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return ChildTexts.get(groupPosition).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return fatherText[groupPosition];
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return ChildTexts.get(groupPosition).size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return groupPosition + childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.one_item_layout, parent, false);
        TextView textView = inflate.findViewById(R.id.etText);
        textView.setText(fatherText[groupPosition]);
        return inflate;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public View getChildView(final int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.one_item_content, parent, false);
        TextView name = inflate.findViewById(R.id.name);
        TextView time = inflate.findViewById(R.id.time);
        TextView type = inflate.findViewById(R.id.type);
        LinearLayout tab = inflate.findViewById(R.id.tab);
        if (groupPosition == 0) {
            final WeChtBeanOut weChtBeanIn = (WeChtBeanOut) ChildTexts.get(groupPosition).get(childPosition);
            name.setText(weChtBeanIn.getText());
            time.setText(weChtBeanIn.getTime());
            type.setText(weChtBeanIn.getType());
            tab.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    goBack.onItemClieck(groupPosition,0);
                }
            });
            tab.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    goBack.longItemClick(weChtBeanIn,groupPosition);
                    return true;
                }
            });
        } else {
            final WeChtBeanIn weChtBeanIn = (WeChtBeanIn) ChildTexts.get(groupPosition).get(childPosition);
            name.setText(weChtBeanIn.getText());
            time.setText(weChtBeanIn.getTime());
            type.setText(weChtBeanIn.getType());
            tab.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    goBack.onItemClieck(groupPosition,0);
                }
            });
            tab.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    goBack.longItemClick(weChtBeanIn,groupPosition);
                    return true;
                }
            });
        }
        return inflate;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}
